package com.guomao.byteStream;

import java.io.*;

public class FileInOutputStreamDemo {


    public static void main(String[] args) throws Exception {


//        new FileInputStream("content.txt");

//        fileread1();

//        fileread2();

//        fileread3();

//        fileread4();
//        long start = System.currentTimeMillis();

//        fileread5();//94

//        fileread4();//133
//        long end = System.currentTimeMillis();
//        System.out.println((end - start) );

//        filewrite1();
//        filewrite2();

//        filewrite3();

//        String src = "D://abc//a.pdf";
//        String dest = "D://xyz//b.pdf";
//        copy(src, dest);

        String src = "D://abc";
        String dest = "D://bak";

        File srcfile = new File(src);//d://abc
        File destfile = new File(dest);//d://bak

        copyPath(srcfile, destfile);
    }

    private static void copyPath(File srcfile, File destfile) {

        if (!srcfile.exists()) {
            throw new RuntimeException("目标文件夹不存在!");
        }

        if (!destfile.exists()) {
            destfile.mkdirs();//创建文件夹
        }

        File[] files = srcfile.listFiles();
        if (files != null) {
            for (File file : files) {
                //D://bak//a.txt
                File targetFile = new File(destfile, file.getName());
                if (file.isDirectory()) {
                    copyPath(file, destfile);
                } else if (file.isFile()) {
                    //file -> a.txt
                    //D://bak//guomaolessons//a.txt
                    copyFile(file, destfile);
                    //a.txt D://bak
                }
            }
        }
    }

    private static void copyFile(File src, File dest) {

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] buf = new byte[1024];
            int len;

            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
                bos.flush();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }


    private static void copy(String src, String dest) {

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(new FileOutputStream(dest));
            byte[] buf = new byte[1024];
            int len;

            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
                bos.flush();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }


    private static void filewrite3() throws Exception {

        //Buffered 高级流 包装流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("buffered.txt"));


        String line = "Hello World  uuid salkadjfakl-lksjdlf-dlsksdf-dfsf";

        bos.write(line.getBytes());

        bos.close();

    }


    private static void filewrite2() {
        FileOutputStream fos = null;
        try {
            //FileOutputStream底层使用了fileSystemmanager 判断该文件是否存在 如果不存在会提前创建
//            fos = new FileOutputStream("outfile.txt");
            fos = new FileOutputStream("outfile.txt", false);
            String str2 = "广东深圳";
            fos.write(str2.getBytes(), 0, 6);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    private static void filewrite1() {
        FileOutputStream fos = null;
        try {
            //FileOutputStream底层使用了fileSystemmanager 判断该文件是否存在 如果不存在会提前创建
//            fos = new FileOutputStream("outfile.txt");
            fos = new FileOutputStream("outfile.txt", false);

            fos.write(97);

            String str1 = "This is a test";
            fos.write(str1.getBytes());

            String str2 = "福建厦门";
//            fos.write(str2.getBytes());
//            fos.write(str2.getBytes("UTF-8"));

//            fos.write(str2.getBytes("GBK"));

//            byte[] bytes = str2.getBytes("UTF-8");
//            System.out.println(Arrays.toString(bytes));

            fos.write(str2.getBytes(), 0, 6);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    private static void fileread5() {
        BufferedInputStream bis = null;
        try {
//            FileInputStream fis = new FileInputStream("content.txt");
//            BufferedInputStream bis = new BufferedInputStream(fis);
            bis = new BufferedInputStream(new FileInputStream("content.txt"));

            byte[] b = new byte[1024];

            int len;

            while ((len = bis.read(b)) != -1) {
//                System.out.print(new String(b, 0, len));

                new String(b, 0, len);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    private static void fileread4() throws IOException {
        File file = new File("content.txt");
        FileInputStream fis = new FileInputStream(file);

        int i = 0;

        byte[] bytes = new byte[1024];

        //[d,e,c]
        // 0 1 2
        while ((i = fis.read(bytes)) != -1) {
            String s = new String(bytes, 0, i);
//            System.out.println(s);
        }


/*        int i1 = fis.read(bytes);

        String s = new String(bytes);
        System.out.println(s);//abc


        System.out.println(i1);
        int i2 = fis.read(bytes);
        System.out.println(i2);
        s = new String(bytes);

        System.out.println(s);//dec*/


    }

    private static void fileread3() throws IOException {


        //关联相对路径下的content.txt
        File file = new File("content.txt");
        FileInputStream fis = new FileInputStream(file);

        //字节数组
        byte[] buf = new byte[1024];
        int len;//每次读取到的有效字节个数
        int count = 0;
        while ((len = fis.read(buf)) != -1) {//读到-1代表读完了文件
            String s = new String(buf, 0, len);
            count++;
            System.out.print(s);
        }

        //关闭流 释放占用的系统资源 防止内存溢出
        fis.close();
        System.out.println("一共读取了" + count + "次");
    }

    private static void fileread2() {
        try {
            File file = new File("content.txt");
            FileInputStream fis = new FileInputStream(file);
//            int count = 0;
            byte[] buffer = new byte[1024];
            //num 当前读取到的有效字符个数
            int num = fis.read(buffer);
//            for (int i = 0; i < buffer.length; i++) {
//                System.out.print((char) buffer[i]);
//            }

            String s = new String(buffer, "UTF-8");
            System.out.println(s);
//            System.out.println("一共读取了" + count + "个字符");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private static void fileread1() {
        try {
            File file = new File("content.txt");

            FileInputStream fis = new FileInputStream(file);
            int i;
            int count = 0;
            while ((i = fis.read()) != -1) {
                count++;
                System.out.print((char) i);
            }
            //14455
            System.out.println("一共读取了" + count + "个字符");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
